package com.wxh.service.impl;

import java.util.Date;
import java.util.List;

import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wxh.common.DataGridView;
import com.wxh.domain.Bills;
import com.wxh.domain.Billtype;
import com.wxh.mapper.BillsMapper;
import com.wxh.mapper.BilltypeMapper;
import com.wxh.service.BillsService;
import com.wxh.vo.BillsVo;

/**
 * <p>
 *  服务实现类
 * </p>
 */
@Service
public class BillsServiceImpl  implements BillsService {
	
	@Autowired
	private BillsMapper billsMapper;
	
	@Autowired
	private BilltypeMapper billtypeMapper;

	@Override
	public DataGridView queryAllBills(BillsVo billsVo) {
		Page<Bills> page=new Page<>(billsVo.getPage(), billsVo.getLimit());
		QueryWrapper<Bills> queryWrapper=new QueryWrapper<>();
		queryWrapper.eq(billsVo.getTypeid()!=null, "typeid", billsVo.getTypeid());
		
		queryWrapper.ge(billsVo.getStartTime()!=null, "billtime", billsVo.getStartTime());
		queryWrapper.le(billsVo.getEndTime()!=null, "billtime", billsVo.getEndTime());
		queryWrapper.orderByDesc("billtime");
		billsMapper.selectPage(page, queryWrapper);
		List<Bills> records = page.getRecords();
		for (Bills bills : records) {
			Billtype billtype = billtypeMapper.selectById(bills.getTypeid());
			bills.setTypename(billtype.getName());
		}
		return new DataGridView(page.getTotal(), records);
	}

	@Override
	public void addBills(Bills bills) {
		bills.setBilltime(new Date());
		this.billsMapper.insert(bills);
	}

}
